package com.ctgu.utils;


import com.ctgu.model.entity.Company;
import com.ctgu.model.entity.Employee;
import com.ctgu.model.entity.LoginUser;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Component
public class ExcelUtil {

    @Autowired
    private  PasswordEncoder passwordEncoder;

    //XXX为excel列对应的实体类
    public static List<Employee> readExcel(MultipartFile file) throws IOException {
        List<Employee> list = new ArrayList<>();
        InputStream inputStream = file.getInputStream(); // 获取上传文件的输入流
        Workbook workbook = WorkbookFactory.create(inputStream); // 使用工厂方法创建 Workbook 对象
        Sheet sheet = workbook.getSheetAt(0); // 获取工作簿中的第一个表格
        DataFormatter dataFormatter = new DataFormatter();
        // 获取表格中的每一行
        // 获取当前行
        boolean isOk = true;
        for (Row currentRow : sheet) {
//            跳过第一行
            if (isOk) {
                isOk = false;
                continue;
            }
            Employee sx = new Employee();
            //获取列赋值给对象，这里就举例，具体的按自己需求定
            sx.setName(dataFormatter.formatCellValue(currentRow.getCell(0)));
            sx.setGender(dataFormatter.formatCellValue(currentRow.getCell(1)));
            sx.setIdCard(dataFormatter.formatCellValue(currentRow.getCell(2)));
            sx.setEmail(dataFormatter.formatCellValue(currentRow.getCell(3)));
            sx.setPhone(dataFormatter.formatCellValue(currentRow.getCell(4)));
            sx.setAddress(dataFormatter.formatCellValue(currentRow.getCell(5)));
            sx.setDepId(Integer.valueOf(dataFormatter.formatCellValue(currentRow.getCell(6))));
            sx.setPosId(Integer.valueOf(dataFormatter.formatCellValue(currentRow.getCell(7))));
            sx.setTiptopDegree(dataFormatter.formatCellValue(currentRow.getCell(8)));
            sx.setSpecialty(dataFormatter.formatCellValue(currentRow.getCell(9)));
            sx.setSchool(dataFormatter.formatCellValue(currentRow.getCell(10)));
            sx.setStatus("1");
            Random random=new Random();
            String userName=sx.getName()+String.valueOf(random.nextInt(1000));
            sx.setUserName(userName);
            sx.setPassword("$2a$10$SvGnKq0DQ3gma9POvR0UtOlhHucj6ZrZToXhk754xYH3CoH0v/b0K");
            sx.setBeginDate(LocalDate.now());
            LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Integer companyId = loginUser.getUser().getId();
            Company company=(Company) loginUser.getUser();
            sx.setCompanyId(companyId);
            sx.setWorkState("在职");
            list.add(sx);
        }
        return list;
    }
}

